
import { defineStore } from 'pinia'
import type { Moment, User } from '@/types'
import { ref } from 'vue'

export const useDiscoverStore = defineStore('discover', () => {
  const moments = ref<Moment[]>([
    {
      id: '1',
      userId: '1',
      user: {
        id: '1',
        username: 'zhangsan',
        nickname: '张三',
        avatar: '/images/avatars/avatar1.jpg',
        signature: '生活就像一盒巧克力'
      },
      content: '今天天气真好，出去走走～',
      images: ['/images/moments/photo1.jpg', '/images/moments/photo2.jpg'],
      timestamp: Date.now() - 3600000,
      likes: ['2', '3'],
      comments: [
        {
          id: '1',
          userId: '2',
          user: {
            id: '2',
            username: 'lisi',
            nickname: '李四',
            avatar: '/images/avatars/avatar2.jpg'
          },
          content: '真不错！',
          timestamp: Date.now() - 1800000
        }
      ],
      isLiked: false
    },
    {
      id: '2',
      userId: '2',
      user: {
        id: '2',
        username: 'lisi',
        nickname: '李四',
        avatar: '/images/avatars/avatar2.jpg',
        signature: '今天天气真好'
      },
      content: '分享一首好听的歌～',
      timestamp: Date.now() - 7200000,
      likes: ['1'],
      comments: [],
      isLiked: true
    }
  ])

  const isLoading = ref(false)

  const likeMoment = (momentId: string, userId: string) => {
    const moment = moments.value.find(m => m.id === momentId)
    if (moment) {
      if (moment.isLiked) {
        moment.likes = moment.likes.filter(id => id !== userId)
        moment.isLiked = false
      } else {
        moment.likes.push(userId)
        moment.isLiked = true
      }
    }
  }

  const addComment = (momentId: string, comment: any) => {
    const moment = moments.value.find(m => m.id === momentId)
    if (moment) {
      moment.comments.push(comment)
    }
  }

  const loadMore = () => {
    isLoading.value = true
    // 模拟加载更多
    setTimeout(() => {
      isLoading.value = false
    }, 1000)
  }

  return {
    moments,
    isLoading,
    likeMoment,
    addComment,
    loadMore
  }
})
